﻿@{
    ViewBag.Title = "RelationOIForm";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}

<script>
    var ObjectId = request('ObjectId');
    var OrganizeId = request('OrganizeId');
    var _TypeId = "";
    var list = [];
    var ids = "";
    $(function () {
        InitialPage();
        GetGrid();
    });
    //初始化页面
    function InitialPage() {
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 114.5);
            }, 200);
            e.stopPropagation();
        });
        //巡检类别
        $("#TypeId").ComboBoxTree({
            url: "../../SystemManage/DataItemDetail/GetDataItemTreeJson",
            param: { EnCode: "PollingType" },
            description: "==请选择==",
            click: function (item) {
                if (item.id != _TypeId) {
                    _TypeId = item.id;
                    $("#gridTable").jqGrid('setGridParam', {
                        url: "../../PropertyManage/CS_Polling/GetItemRelationOIListJson?ObjectId=" + ObjectId + "&TypeId=" + _TypeId + "&OrganizeId=" + OrganizeId,
                    }).trigger('reloadGrid');
                }
            }
        });
    }
    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            url: "../../PropertyManage/CS_Polling/GetItemRelationOIListJson?ObjectId=" + ObjectId + "&TypeId=" + _TypeId + "&OrganizeId=" + OrganizeId,
            datatype: "json",
            height: $(window).height() - 114.5,
            autowidth: true,
            multiselect: true,
            cellEdit: true,
            cellsubmit: 'rclientArray',
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: '主键', name: 'OIId', hidden: true },
                { label: '主键', name: 'isCheck', hidden: true },
                { label: '主键', name: 'Type', hidden: true },
                { label: '项目名称', name: 'Name', index: 'ItemName', width: 200, align: 'center', sortable: false },
                { label: '编码', name: 'Code', index: 'ItemValue', width: 200, align: 'center', sortable: false },
                { label: '类别', name: 'TypeName', index: 'SimpleSpelling', width: 150, align: 'center', sortable: false },
                {
                    label: "执行频次", name: "Frenquency", index: "Frenquency", width: 300, align: "left",
                    formatter: function (cellvalue, options, rowObject) {
                        switch (rowObject.Unit) {
                            case "0": return rowObject.UnitNum + "天" + cellvalue + "次";
                            case "1": return rowObject.UnitNum + "周" + cellvalue + "次";
                            case "2": return rowObject.UnitNum + "月" + cellvalue + "次";
                            case "3": return rowObject.UnitNum + "季" + cellvalue + "次";
                            case "4": return rowObject.UnitNum + "年" + cellvalue + "次";
                        }
                    }
                },
                { label: '执行次数', name: 'Frequency', index: 'Frequency', editable: true, editrules: { edithidden: true, required: true, number: true }, edittype: "text", width: 150, align: 'center', sortable: false }
            ],
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function (rowid, status) {
                //selectedRowIndex = $("#" + this.id).getGridParam('selarrrow');
                if (!!$gridTable.getRowData(rowid).OIId) {
                    if (status) {
                        $.each(list, function (i, n) {
                            if ($gridTable.getRowData(rowid).Id == n.Id) {
                                $gridTable.setRowData(rowid, n);
                                list.splice(i, 1);
                                return false;
                            }
                        });
                        list.push($gridTable.getRowData(rowid));
                        ids.replace($gridTable.getRowData(rowid).OIId + ",", "");
                    }
                    else {
                        $.each(list, function (i, n) {
                            if ($gridTable.getRowData(rowid).Id == n.Id) {
                                list.splice(i, 1);
                                return false;
                            }
                        });
                        ids += $gridTable.getRowData(rowid).OIId + ","
                    }
                }
                else {
                    if (status) {
                        list.push($gridTable.getRowData(rowid));
                    }
                    else {
                        $.each(list, function (i, n) {
                            if ($gridTable.getRowData(rowid).Id == n.Id) {
                                list.splice(i, 1);
                                return false;
                            }
                        });
                    }
                }
            },
            onSelectAll: function (arowid, status) {
                for (var i = 0; i < list.length; i++) {
                    if (_TypeId == list[i].Type) {
                        list.splice(i, 1);//从下标为i的元素开始，连续删除1个元素
                        i--;//因为删除下标为i的元素后，该位置又被新的元素所占据，所以要重新检测该位置
                    }
                }
                var rowIds = $gridTable.jqGrid('getDataIDs');
                $.each(rowIds, function (i, n) {
                    if (!!$gridTable.getRowData(n).OIId) {
                        if (status) {
                            list.push($gridTable.getRowData(n));
                            ids.replace($gridTable.getRowData(n).OIId + ",", "");
                        }
                        else {
                            ids += $gridTable.getRowData(n).OIId + ","
                        }
                    }
                    else {
                        if (status) {
                            list.push($gridTable.getRowData(n));
                        }
                        else {
                            $.each(list, function (i, a) {
                                if ($gridTable.getRowData(n).Id == a.Id) {
                                    list.splice(i, 1);
                                    return false;
                                }
                            });
                        }
                    }
                });
            },
            beforeSelectRow: function (rowid, e) {
                var $myGrid = $(this),
                    i = $.jgrid.getCellIndex($(e.target).closest('td')[0]),
                    cm = $gridTable.jqGrid('getGridParam', 'colModel');
                return (cm[i].name === 'cb');
            },
            gridComplete: function () {
                //$("#" + this.id).setSelection(selectedRowIndex, false);
                var rowIds = $gridTable.jqGrid('getDataIDs');
                for (var k = 0; k < rowIds.length; k++) {
                    var data = $gridTable.getRowData(rowIds[k]);
                    if (data.isCheck == "true") {
                        $gridTable.setSelection(rowIds[k]);
                        if (ids.indexOf(data.Id) >= 0) { ids.replace($gridTable.getRowData(rowIds[k]).OIId + ",", ""); $gridTable.setSelection(rowIds[k]); }
                    }
                    else {
                        $.each(list, function (i, n) {
                            if (data.Id == n.Id && !n.OIId) {
                                $gridTable.setRowData(rowIds[k], n);
                                list.splice(i, 1);
                                $gridTable.setSelection(rowIds[k]);
                                return false;
                            }
                        });
                    }
                }
            },
            ondblClickRow: function (rowid, iRow, iCol, e) {
                if (rowid && rowid !== selectedRowIndex && selectedRowIndex != 0) {
                    $gridTable.saveRow(selectedRowIndex);
                    $.each(list, function (i, n) {
                        if ($gridTable.getRowData(rowid).Id == n.Id) {
                            list[i].Frequency = $gridTable.getRowData(rowid).Frequency;
                            return false;
                        }
                    });
                }
                $gridTable.editRow(rowid, {
                    keys: true, successfunc: function (response) {
                        $gridTable.jqGrid("saveRow", rowid);
                        $.each(list, function (i, n) {
                            if ($gridTable.getRowData(rowid).Id == n.Id) {
                                list[i].Frequency = $gridTable.getRowData(rowid).Frequency;
                                return false;
                            }
                        });
                        return true;
                    }
                });
                selectedRowIndex = rowid;
            }
        });
    }

    //保存表单
    function AcceptClick() {
        if (list.length < 1) {
            dialogMsg('最少选择一条数据', 0);
            return false;
        }
        var Objlist = [];
        $.each(list, function (i, n) {
            var a = { Id: n.OIId, ItemId: n.Id, TypeId: n.Type, ObjectId: ObjectId, Frenquency: n.Frequency, OrganizeId: OrganizeId };
            Objlist.push(a);
        })

        $.SaveForm({
            url: "../../PropertyManage/CS_Polling/SaveRelationOIForm",
            param: { objlist: JSON.stringify(Objlist), ids: ids },
            loading: "正在保存数据...",
            success: function () {
                $.currentIframe().FooTable();
            }
        })
    }

</script>
<div class="titlePanel">
    <div class="title-search">
        <table>
            <tr>
                <td>
                    巡检类别
                </td>
                <td style="padding-left: 5px;">
                    <div id="TypeId" type="selectTree" class="ui-select" style="width: 200px;"></div>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="gridPanel">
    <table id="gridTable"></table>
</div>
